package com.heatingcharge.service.base.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heatingcharge.common.exception.BaseException;
import com.heatingcharge.common.util.BeanValidator;
import com.heatingcharge.common.util.ResponseCodeEnum;
import com.heatingcharge.dto.ChargeDiscountDTO;
import com.heatingcharge.entity.base.ChargeDiscount;
import com.heatingcharge.entity.base.HeatYear;
import com.heatingcharge.mapper.base.ChargeDiscountMapper;
import com.heatingcharge.mapper.base.HeatYearMapper;
import com.heatingcharge.param.ChargeDiscountParam;
import com.heatingcharge.security.JwtUser;
import com.heatingcharge.security.util.SecurityUtil;
import com.heatingcharge.service.base.IChargeDiscountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 预缴优惠表 服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-10-25
 */
@Service
public class ChargeDiscountServiceImpl extends ServiceImpl<ChargeDiscountMapper, ChargeDiscount> implements IChargeDiscountService {

    @Autowired
    HeatYearMapper heatYearMapper;

    @Override
    public List<ChargeDiscountDTO> getChargeDiscounts(ChargeDiscountDTO discountDTO) {
        return baseMapper.selectChargeDiscount(discountDTO);
    }

    @Override
    public Integer save(ChargeDiscountParam chargeDiscountParam) {
        //校验字段合法性
        BeanValidator.check(chargeDiscountParam);
        //校验优惠起止日期是否越界
        if (disDateOverStep(chargeDiscountParam)){
            throw new BaseException(ResponseCodeEnum.DISDATE_OVERSTEP.getMessage(),ResponseCodeEnum.DISDATE_OVERSTEP.getCode());
        }
        //检验优惠起止日期是否已经存在
        if (checkExist(chargeDiscountParam)){
            throw new BaseException(ResponseCodeEnum.DISDATE_EXIST.getMessage(),ResponseCodeEnum.DISDATE_EXIST.getCode());
        }
        JwtUser user = SecurityUtil.getUser();
        ChargeDiscount chargeDiscount=ChargeDiscount.builder()
                .heatYearId(chargeDiscountParam.getHeatYearId())
                .disMode(chargeDiscountParam.getDisMode())
                .disValue(chargeDiscountParam.getDisValue())
                .startDate(chargeDiscountParam.getStartDate())
                .endDate(chargeDiscountParam.getEndDate())
                .companyId(chargeDiscountParam.getCompanyId())
                .createdBy(user.getRealName())
                .build();
        return baseMapper.insert(chargeDiscount);
    }

    /**
     * 校验参数起止日期是否在缴费起止日期之内
     * @param chargeDiscountParam
     * @return
     */
    private boolean disDateOverStep(ChargeDiscountParam chargeDiscountParam) {
        QueryWrapper<HeatYear> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",chargeDiscountParam.getHeatYearId());
//        queryWrapper.;
        queryWrapper.le("charge_start",chargeDiscountParam.getStartDate());
        queryWrapper.ge("charge_end",chargeDiscountParam.getEndDate());
        HeatYear heatYear = heatYearMapper.selectOne(queryWrapper);
        return heatYear ==null;
    }
    private boolean checkExist(ChargeDiscountParam chargeDiscountParam) {
        QueryWrapper<ChargeDiscount> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("heat_year_id",chargeDiscountParam.getHeatYearId());
        queryWrapper.le("start_date",chargeDiscountParam.getEndDate());
        queryWrapper.ge("end_date",chargeDiscountParam.getStartDate());
        queryWrapper.eq("del_flag",0);
        if (chargeDiscountParam.getId()!=null&&chargeDiscountParam.getId()!=0){
            queryWrapper.ne("id",chargeDiscountParam.getId());
        }
        ChargeDiscount chargeDiscount = baseMapper.selectOne(queryWrapper);
        return chargeDiscount !=null;
    }
    @Override
    public Integer update(ChargeDiscountParam chargeDiscountParam) {
        //校验字段合法性
        BeanValidator.check(chargeDiscountParam);
        //校验优惠起止日期是否越界
        if (disDateOverStep(chargeDiscountParam)){
            throw new BaseException(ResponseCodeEnum.DISDATE_OVERSTEP.getMessage(),ResponseCodeEnum.DISDATE_OVERSTEP.getCode());
        }
        //检验年度是否已经存在
        if (checkExist(chargeDiscountParam)){
            throw new BaseException(ResponseCodeEnum.DISDATE_EXIST.getMessage(),ResponseCodeEnum.DISDATE_EXIST.getCode());
        }
        ChargeDiscount before=baseMapper.selectById(chargeDiscountParam.getId());
        //待更新的不存在
        if (ObjectUtil.isNull(before)){
            throw new BaseException(ResponseCodeEnum.CHARGEPRICE_NON_EXIST.getMessage(),ResponseCodeEnum.CHARGEPRICE_NON_EXIST.getCode());
        }
        ChargeDiscount after =new ChargeDiscount();
        BeanUtils.copyProperties(chargeDiscountParam,after);
        JwtUser user = SecurityUtil.getUser();
        after.setUpdatedBy(user.getRealName());
        return baseMapper.updateById(after);
    }

    @Override
    public Integer delete(Integer id) {
        JwtUser user = SecurityUtil.getUser();
        ChargeDiscount chargeDiscount=ChargeDiscount.builder()
                .id(id)
                .delFlag(1)
                .updatedBy(user.getRealName())
                .build();

        return baseMapper.updateById(chargeDiscount);
    }
}
